package cn.gpnu.dao;

import cn.gpnu.domain.Tree;
import cn.gpnu.domain.TreePrice;
import cn.gpnu.domain.User;
import cn.gpnu.util.JDBCUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class TreePriceDao {
    //声明JDBCTemplate对象共用

    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDateSource());


    public List<TreePrice> select_price(){
        try {
            //1编写sql
            String sql = "select * from tree";
//            //2调用query方法
//            List<Map<String, Object>> list = template.queryForList(sql, id);
//
//            return list;
            List<TreePrice> query = template.query(sql, new RowMapper<TreePrice>() {
                @Override
                public TreePrice mapRow(ResultSet arg0, int arg1) throws SQLException {
                    TreePrice treePrice = new TreePrice();
                    treePrice.setId(arg0.getInt("id"));
                    treePrice.setTree_name(arg0.getString("tree_name"));
                    treePrice.setTree_price(arg0.getString("tree_price"));
//                    p.setPname(arg0.getString("pname"));
//                    p.setPrice(arg0.getDouble("price"));
//                    return p;
                    return treePrice;
                }
            });

//           for (TreePrice treePrice : query) {
//               System.out.println(treePrice.getTree_name()+":");
//               System.out.println(treePrice.getTree_price());
//            }
            return query;
        } catch (DataAccessException e) {
            // e.printStackTrace();
            System.out.println("抹油查到");
            return null;
        }
    }
}
